package org.jeecg.modules.quartz.job;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.qixin.modules.ea.entity.EaCostItem;
import com.qixin.modules.ea.service.IEaCostItemService;
import org.apache.commons.lang3.StringUtils;
import org.jeecg.common.nc.enums.WSEnum;
import org.jeecg.common.nc.service.NCWebService;
import org.jeecg.common.nc.vo.WSVO;
import org.jeecg.modules.nc.entity.NcInoutbusiclass;
import org.jeecg.modules.nc.entity.NcInoutuse;
import org.jeecg.modules.nc.entity.NcOrg;
import org.jeecg.modules.nc.service.INcInoutbusiclassService;
import org.jeecg.modules.nc.service.INcInoutuseService;
import org.jeecg.modules.quartz.entity.TbJobLog;
import org.jeecg.modules.quartz.service.IQuartzJobService;
import org.jeecg.modules.quartz.service.ITbJobLogService;
import org.jeecg.modules.system.service.impl.SysDepartServiceImpl;
import org.quartz.Job;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author: wjb
 * @Date: 2021/12/19/17:53
 * @Description: NC收支项目同步接口
 */
public class NCInoutBusicClassSyncJob implements Job {

    private static final Logger logger = LoggerFactory.getLogger(NCUserSyncJob.class);

    @Autowired
    IQuartzJobService jobService;
    @Autowired
    private INcInoutbusiclassService ncInoutbusiclassService;
    @Autowired
    private INcInoutuseService ncInoutuseService;
    @Autowired
    SysDepartServiceImpl sysDepartService;
    @Autowired
    private ITbJobLogService tbJobLogService;

    @Value("${qixin.ncserver.uri}")
    private String NC_SERVER_URI;

    @Override
    public void execute(JobExecutionContext jobExecutionContext) throws JobExecutionException {

        try {
            String lastTime = jobService.queryLastTime("ncinoutbasicclasssyncjob");
            String currentTime = DateUtil.now();
            Boolean first = false;
            if (StrUtil.isBlank(lastTime)){
                lastTime = "2008-01-01 00:00:00";
                first = true; // 表示第一次同步
            }
            
            // 获取组织个数
            List<NcOrg> ncOrgs = sysDepartService.querOrgList();
            WSVO vo = new WSVO();
            if(ncOrgs.size() > 0){
                for (NcOrg ncOrg:ncOrgs) {
                    WSEnum.INOUTBASICCLASS.setMsg(ncOrg.getCode());
                    vo = NCWebService.getFromWS(NC_SERVER_URI, WSEnum.INOUTBASICCLASS,lastTime);
                    for (Map<String, Object> map : vo.getContent()) {
                        NcInoutbusiclass ncInoutbusiclass = new NcInoutbusiclass();
                        ncInoutbusiclass.setId((String) map.get("pk_inoutbusiclass")); // 收支项目主键
                        ncInoutbusiclass.setName((String) map.get("name")); //  收支项目名称
                        ncInoutbusiclass.setPkParent((String) map.get("pk_parent")); // 上级收支项目
                        ncInoutbusiclass.setEnablestate((Integer) map.get("enablestate")); // 启用状态
                        ncInoutbusiclass.setCode((String) map.get("code")); // 收支项目编码
                        ncInoutbusiclass.setPkGroup((String) map.get("pk_group")); // 所属集团
                        ncInoutbusiclass.setPkOrg((String) map.get("pk_org")); // 所属组织
                        ncInoutbusiclass.setDr((Integer) map.get("dr")); // 是否删除

                        ncInoutbusiclassService.saveOrUpdate(ncInoutbusiclass);
                    }
                    TbJobLog tbJobLog = new TbJobLog();
                    tbJobLog.setName(ncOrg.getName()+"NC收支项目同步");
                    tbJobLog.setContent(ncOrg.getName()+"本次新增或更新记录" + vo.getContent().size() + "条");
                    tbJobLog.setNum(vo.getContent().size());
                    tbJobLogService.save(tbJobLog);
                }
                if (first) {
                    // 新增一条记录
                    jobService.insetLastTime("ncinoutbasicclasssyncjob", currentTime);
                } else {
                    //更新最后同步时间
                    jobService.updateLastTime("ncinoutbasicclasssyncjob", currentTime);
                }
                logger.info("NC收支项目同步成功更新：{}条", vo.getContent().size());
            }

        }catch (Exception e){
            logger.info("NC收支项目同步出现异常：{}", e.getMessage());
            e.printStackTrace();
            TbJobLog tbJobLog = new TbJobLog();
            tbJobLog.setName("NC收支项目同步");
            tbJobLog.setContent("同步出现异常：" + e.getMessage());
            tbJobLog.setNum(0);
            tbJobLogService.save(tbJobLog);
        }

        
        // update-begin-by wujinbiao 2022/2/26 10:53 for NC收支项目使用权同步
        try {
            String lastTime = jobService.queryLastTime("ncinoutusesyncjob");
            String currentTime = DateUtil.now();
            Boolean first = false;
            if (StrUtil.isBlank(lastTime)){
                lastTime = "2008-01-01 00:00:00";
                first = true; // 表示第一次同步
            }

            // 获取组织个数
            List<NcOrg> ncOrgs = sysDepartService.querOrgList();
            WSVO vo = new WSVO();
            if(ncOrgs.size() > 0){
                for (NcOrg ncOrg:ncOrgs) {
                    WSEnum.INOUTUSE.setMsg(ncOrg.getCode());
                    vo = NCWebService.getFromWS(NC_SERVER_URI, WSEnum.INOUTUSE,lastTime);
                    for (Map<String, Object> map : vo.getContent()) {
                        NcInoutuse ncInoutuse = new NcInoutuse();
                        ncInoutuse.setId((String) map.get("pk_inoutuse")); // 收支项目授权主键
                        ncInoutuse.setDataoriginflag((Integer) map.get("dataoriginflag")); // 数据来源
                        ncInoutuse.setPkInoutbusiclass((String) map.get("pk_inoutbusiclass")); // 收支项目主键
                        ncInoutuse.setPkGroup((String) map.get("pk_group")); // 所属集团
                        ncInoutuse.setPkOrg((String) map.get("pk_org")); // 所属组织
                        ncInoutuse.setEnablestate((Integer) map.get("enablestate")); // 启用状态
                        ncInoutuse.setDr((Integer) map.get("dr")); // 删除状态

                        ncInoutuseService.saveOrUpdate(ncInoutuse);
                    }
                    TbJobLog tbJobLog = new TbJobLog();
                    tbJobLog.setName(ncOrg.getName()+"NC收支项目使用权同步");
                    tbJobLog.setContent(ncOrg.getName()+"本次新增或更新记录" + vo.getContent().size() + "条");
                    tbJobLog.setNum(vo.getContent().size());
                    tbJobLogService.save(tbJobLog);
                }
                if (first) {
                    // 新增一条记录
                    jobService.insetLastTime("ncinoutusesyncjob", currentTime);
                } else {
                    //更新最后同步时间
                    jobService.updateLastTime("ncinoutusesyncjob", currentTime);
                }
                logger.info("NC收支项目使用权同步成功更新：{}条", vo.getContent().size());
            }

        }catch (Exception e){
            logger.info("NC收支项目使用权同步出现异常：{}", e.getMessage());
            e.printStackTrace();
            TbJobLog tbJobLog = new TbJobLog();
            tbJobLog.setName("NC收支项目使用权同步");
            tbJobLog.setContent("同步出现异常：" + e.getMessage());
            tbJobLog.setNum(0);
            tbJobLogService.save(tbJobLog);
        }
        // update-end-by wujinbiao 2022/2/26 10:53 for NC收支项目使用权同步

    }
}
